#include "SeqList.h"


//初始化线性表
void SeqListInit(SL* ps)
{
	ps->L = NULL;
	ps->size = 0;
	ps->capacity = 0;
}
//打印线性表
void SeqListPrint(SL* ps)
{
	for (int i = 0; i < ps->size; i++)
	{
		printf("%d ", ps->L[i]);
	}
	printf("\n");
}
//扩容操作
void SeqListCheckCapacity(SL* ps)
{
	if (ps->size == ps->capacity)
	{
		int new_capacity = ps->capacity == 0 ? 4 : ps->capacity * 2;
		DateType *tmp=(DateType*)realloc(ps->L, new_capacity * (sizeof(DateType)));
		if (tmp == NULL)
		{
			printf("relloc is fail\n");
			exit(-1);
		}
		else
		{
			ps->L = tmp;
			ps->capacity = new_capacity;
		}
	}
}
//尾插
void SeqListPushBack(SL* ps, DateType x)
{
	SeqListCheckCapacity(ps);
	ps->L[ps->size] = x;
	ps->size++;
}
//头插
void SeqListPushFront(SL* ps, DateType x)
{
	SeqListCheckCapacity(ps);
	int end = ps->size - 1;
	while (end>=0)
	{
		ps->L[end+1] = ps->L[end];
		end--;
	}
	ps->L[0] = x;
	ps->size++;
}
//尾删
void SeqListPopBack(SL* ps)
{
	assert(ps->size > 0);
	ps->size--;
}
//头删
void SeqListPopFront(SL* ps)
{
	assert(ps->size > 0);
	int front=1;
	while (front < ps->size)
	{
		ps->L[front - 1] = ps->L[front];
		front++;
	}
	ps->size--;
}
//任意位置插入
void SeqListInsert(SL* ps, int pos, DateType x)
{
	assert(pos < ps->size);
	SeqListCheckCapacity(ps);
	int end = ps->size;
	while (pos<end)
	{
		ps->L[end] = ps->L[end - 1];
		end--;
	}
	ps->L[pos] = x;
	ps->size++;
}
//任意位置删除
void SeqListRemove(SL* ps, int pos)
{
	assert(pos < ps->size);
	while (pos < ps->size)
	{
		ps->L[pos] = ps->L[pos + 1];
		pos++;
	}
	ps->size--;
}
